package com.example.demo.Controller;

import com.example.demo.Dto.AddItemDTO;
import com.example.demo.Info.CartItemInfo;
import com.example.demo.Service.CartItemService;
import com.example.demo.Service.CartService;
import com.example.demo.Util.ThreadLocallUtil;
import com.example.demo.VO.CartInfoVo;
import com.example.demo.VO.CartItem;
import com.example.demo.po.User;
import com.example.demo.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/carts")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private CartItemService cartItemService;
    /**
     * 添加购物车
     * @param addItemDTO
     * @return
     */
    @PostMapping
    public ResponseResult Postcart(@RequestBody AddItemDTO addItemDTO){
        User user = ThreadLocallUtil.getUser();
        Long userId = user.getUserId();
        Long l = cartService.addCart(userId);
        cartItemService.addCart(addItemDTO,l);
        return new  ResponseResult().ok(addItemDTO);
    }
  /**
     * 删除购物车
     * @return
     */
    @DeleteMapping
    public ResponseResult deleteCart(){
        User user = ThreadLocallUtil.getUser();
        Long userId = user.getUserId();
        Long l = cartService.addCart(userId);
        cartItemService.deleteCart(l,userId);
        return new ResponseResult().ok();
    }
    /**
     * 获取购物车
     * @return
     */
    @GetMapping
    public ResponseResult getCart(){
        User user = ThreadLocallUtil.getUser();
        Long userId = user.getUserId();
        CartInfoVo getcart = cartService.getcart(userId);
        return new ResponseResult().ok(getcart);
    }

    /**
     * 获得某个购物车商品ID和数量
     * @param id
     * @return
     */
    @GetMapping("/items/{id}")
    public ResponseResult getCartItem(@PathVariable Long id){
        CartItem cartItem = cartItemService.idgetCartItem(id);
        return new ResponseResult().ok(cartItem,"获取购物车项成功");
    }
}
